<?php
namespace app\api\controller;

use think\facade\Db;
use app\api\controller\ApiComData;
use think\facade\Request;

/**
 * 2024-12-14 huangzhen
 * 异步定时处理卡号相关事务
 * Class TimerCards
 * @package app\api\controller
 */
class TimerCards
{
    public function __construct()
    {
        $params = Request::param();
        if(empty($params)){
            exit('Welcome to BetterEpay');
        }else{
            $token = $params['token'];
            //4ba0229e63c4a0ec6ba5e999cba5d586
            if($token !=md5(md5('BetterEpay') . md5(2022))){
                exit('Welcome to BetterEpay !');
            }
        }
    }

    /**
     * 非系统卡号:
     * 1.通过card_token获取card_num,
     * 2.同时冻结卡号
     */
    public function getCardNum()
    {
        //便利非系统卡列表，整合出已经有卡号的card_token
        $list_all = Db::name('cards_trans_new')->where("card_num != ''")->select()->toArray();
        $card_num_array = [];
        if(!empty($list_all)){
            foreach ($list_all as $k_all=>$v_all){
                if(!isset($card_num_array[$v_all['mcc']])){
                    $card_num_array[$v_all['mcc']]['card_num'] = $v_all['card_num'];
                    $card_num_array[$v_all['mcc']]['card_status'] = $v_all['ctime_real'];
                    $card_num_array[$v_all['mcc']]['error_msg'] = $v_all['utime_real'];
                }
            }
        }
        //处理
        $list = Db::name('cards_trans_new')
            ->where("ctime_real is null and utime_real !=''")
            ->order('id asc')
            ->limit(20)->select()->toArray();
        foreach ($list as $k=>$v){
            $id         = $v['id'];
            $card_token = $v['mcc'];
            $customer_str = $v['mid'];
            if(!empty($customer_str)){
                $customer_array = explode(',',$customer_str);
                $customerCode   = $customer_array[1];
                $customerId     = $customer_array[0];

                if(isset($card_num_array[$v['mcc']])){
                    Db::name('cards_trans_new')
                        ->where(['id'=>$id])
                        ->update([
                            'card_num'  =>$card_num_array[$v['mcc']]['card_num'],
                            'ctime_real'=>$card_num_array[$v['mcc']]['card_status'],
                            'utime_real'=>$card_num_array[$v['mcc']]['error_msg'],
                            ]);
                }else{
                    //第一步，首先查询卡详情
                    $send_param         = [
                        "action_type"       => 'searchCard2Token',
                        "card_token"        => $card_token,
                        "customerCode"      => $customerCode,
                        "customerId"        => $customerId,
                    ];
                    $check_card_api = PublicApi::requestApi('Comdata2',$send_param);
                    $return_blog    = $check_card_api['blog'];
                    $return_list    = $check_card_api['list'];
                    if($return_blog){
                        $cardStatus = $return_list['cardStatus'];
                        $cardNumber = $return_list['cardNumber'];
                        $error_msg  = '';
                        //②如果卡是激活状态 冻结卡
                        if($cardStatus == 'A'){
                            $send_param_ice         = [
                                "action_type"       => 'card_suspended',
                                "card_num"          => $cardNumber,
                                "customerCode"      => $customerCode,
                                "customerId"        => $customerId,
                            ];
                            $card_api_ice = PublicApi::requestApi('Comdata2',$send_param_ice);
                            $return_ice_blog    = $card_api_ice['blog'];
                            $return_ice_list    = $card_api_ice['list'];
                            if($return_ice_blog){
                                $cardStatus = 'B';
                            }else{
                                $error_msg  = $return_ice_list['error_string'] ?? '';
                            }
                        }
                        Db::name('cards_trans_new')
                            ->where(['id'=>$id])
                            ->update([
                                'card_num'      => $cardNumber,
                                'ctime_real'    => $cardStatus,
                                'idMatchNumber' => $error_msg
                            ]);
                    }
                }
            }else{
                continue;
            }
        }

    }

}
